package com.huang.LockSupport;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


/*
*       等待和唤醒  必须要被锁住     而且必须 先有等待  才能有唤醒
*
* */
public class test2 {
    public static void main(String[] args) throws InterruptedException {
        Object o = new Object();
        Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();
        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            lock.lock();
            try {
                System.out.println("w前");
                condition.await();
                System.out.println("w后");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                lock.unlock();
            }

        }).start();

        TimeUnit.SECONDS.sleep(2);

        new Thread(()->{
            lock.lock();
            try {
                System.out.println("N前");
                condition.signal();
                System.out.println("N后");

            }finally {
                lock.unlock();
            }

        }).start();
    }
}
